<?php
/**
 * Created by PhpStorm.
 * User: yuyiwen
 * Date: 2018/9/22
 * Time: 上午9:50
 */

namespace app\admin\controller;


use think\Controller;
use think\Session;

class Log extends Controller
{
    public function login ()
    {
        return view();
    }

    public function checklog()
    {
//        dump(input('post.'));
        $post['username'] = input('post.username','','htmlspecialchars');
        $post['pwd'] = input('post.pwd','','htmlspecialchars');
        # 此处省略了验证
        $post['pwd'] = md5($post['pwd']);

        $hasUser = db('admin')
            ->where($post)
            ->find();
        if (!$hasUser) {
            $this->error('用户名或密码错误');
        }
//        session(['expire' => 7200]);
        session('admin_id' , $hasUser['id']);

        // 查询会员角色
        $role_list = db('AdminRole')->where('admin_id' , session('admin_id')) -> select();
        $roles = [];
        foreach ($role_list as $key => $value) {
            $roles[] = $value['role_id'];
        }

        $roles = implode($roles , ',');
        // 将用户的角色存储到session中
        session('roles' , $roles);
        session('admin_name' , $hasUser['username']);

        $this->success('登录成功！', url('index/index'));
    }

    public function logout ()
    {
        Session::delete('admin_id');
        Session::delete('admin_name');
        Session::delete('roles');
        $this->error('退出成功' , url('login'));
    }

}